package org.jvending.provisioning.portal;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.jvending.provisioning.portal.impl.BundleDescriptorConverterImpl;

import javax.provisioning.BundleDescriptor;
import javax.provisioning.BundleRepository;
import javax.provisioning.ProvisioningContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

public class ContentQuerierServlet extends HttpServlet {

    private static Logger logger = Logger.getAnonymousLogger();

    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
        String keyword = request.getParameter("keyword");
        ProvisioningContext provisioningContext = (ProvisioningContext)
                this.getServletContext().getAttribute("javax.provisioning.ProvisioningContext");

        Directory directory = FSDirectory.getDirectory(System.getProperty("java.io.tmpdir") + "/index", false);
        IndexSearcher indexSearcher = new IndexSearcher(directory);
        logger.info("JV: Search Parameter = " + keyword);

        Query query = null;
        try {
            QueryParser parser = new QueryParser("description", new StandardAnalyzer());
            query = parser.parse(keyword);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Hits hits = indexSearcher.search(query);
        logger.info("Hits = " + hits.length());
        List<BundleDescriptor> bundleDescriptors = new ArrayList<BundleDescriptor>();
        BundleRepository bundleRepository = provisioningContext.getBundleRepository();
        for (int i = 0; i < hits.length(); i++) {
            Document document = hits.doc(i);
            String bundleId = document.getField("id").stringValue();
            BundleDescriptor bundleDescriptor = bundleRepository.getBundleByID(bundleId);
            bundleDescriptors.add(bundleDescriptor);
        }
        directory.close();
        indexSearcher.close();

        BundleDescriptorConverter converter = new BundleDescriptorConverterImpl();
        //   ProvisioningArchive provisioningArchive =
        //       converter.toProvisioningArchive(bundleDescriptors, null, "http://localhost:8080/provisioning-portal-webapp");
        // logger.info("Obtained Provisioning Archive");
        /**
         StringWriter writer = new StringWriter();
         try {
         JAXBContext jc = JAXBContext.newInstance("org.jvending.provisioning.stocking.par");
         Marshaller m = jc.createMarshaller();
         m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
         m.setProperty(Marshaller.JAXB_ENCODING, "ISO-8859-1");
         logger.info("AB");
         m.marshal(provisioningArchive, System.out);
         logger.info("B");
         } catch (JAXBException e) {
         logger.log(Level.INFO, "JV-1852-007: Problem with provisioning descriptor", e);
         }
         */
        response.setContentType("text/xml");
        OutputStream os = response.getOutputStream();
        os.write(converter.toProvisioningArchiveString(bundleDescriptors, null, "http://localhost:8080/provisioning").getBytes());
        os.close();
    }


}
